package tool.ducong.com.mygpsdemo;

import android.app.Application;
import android.content.Context;
import android.location.Criteria;
import android.os.Handler;
import android.os.Message;

import com.baidu.mapapi.SDKInitializer;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import mytool.Data;
import mytool.Itude;
import mytool.PrintLog;
import mytool.ShardPreferencesTool;

/**
 * Created by zerdoor_pc on 2016/3/3.
 */
public class MyAPP extends Application {
    public static Itude itude;
//    public static Itude olditude;
    public static Itude differentialitude;
    public static Handler Basehandler;
    public static Handler MobileActivityhandler;
    public static Handler Layerhandler;
    public static int time=1;
    public static int POWER= Criteria.POWER_MEDIUM;
    public static int activityflag=0;
    public static Data Data;
    public static Context mContext;
    public static List<Itude> list_point;//存储的点集
    public static String LIST_POINT="ALL_POINT";
    public static Double ACCURACY=100.0;//GPS点集最大距离
    public static Double OldACCURACY;//GPS点集最大距离
    public static Double MinACCURACY=3.0;//GPS点集最大距离
    public static int Minimum=5;//GPS点集最小个数
    public static int Maximum=100;//GPS点集最大个数
    public static Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    PrintLog.i("HTTPURL:"+"发送hander");
                    switch (activityflag){
                        case 0:

                            break;
                        case 1:
                            if (MobileActivityhandler!=null){MobileActivityhandler.sendEmptyMessage(0);}
                            if (Layerhandler!=null){
                                Layerhandler.sendEmptyMessage(0);
                            }
                            break;
                        case 2:
                            //if (Basehandler!=null){Basehandler.sendEmptyMessage(0);}
                            break;
                        case 3:
                            break;
                        default:
                            break;
                    }




                    break;
                default:
                    break;
            }
        }
    };
    @Override
    public void onCreate() {
        super.onCreate();
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        OldACCURACY=ACCURACY;
        Data=new Data();
        mContext = getApplicationContext();
        POWER=ShardPreferencesTool.getshare(getApplicationContext(), "MyAPP.POWER", Criteria.POWER_MEDIUM);
        time=ShardPreferencesTool.getshare(this,"update_time", 3);
    }



    /**
     * 文件存储命名
     */
    public static String getJsonFileName(String File, int ID) {
        return File + ID + ".json";
    }

    public static String getJsonFileName(String File) {
        return File + ".json";
    }

    public static void fileWrite(String filename, String Content) {
        FileOutputStream out = null;
        try {
            out = mContext.openFileOutput(filename, mContext.MODE_PRIVATE);
            out.write(Content.getBytes("UTF-8"));
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static String fileRead(String filename) {
        FileInputStream in = null;
        ByteArrayOutputStream bout = null;
        byte[] buf = new byte[1024];
        bout = new ByteArrayOutputStream();
        int length = 0;
        try {
            in = mContext.openFileInput(filename); //获得输入流
            while ((length = in.read(buf)) != -1) {
                bout.write(buf, 0, length);
            }
            byte[] content = bout.toByteArray();
            return new String(content, "UTF-8"); //设置文本框为读取的内容
        } catch (Exception e) {
            try {
                in.close();
                bout.close();
            } catch (Exception x) {

            }
            return "";
        }

    }

    public static String fileRead(InputStream in) {
        ByteArrayOutputStream bout = null;
        byte[] buf = new byte[1024];
        bout = new ByteArrayOutputStream();
        int length = 0;
        try {
            while ((length = in.read(buf)) != -1) {
                bout.write(buf, 0, length);
            }
            byte[] content = bout.toByteArray();
            return new String(content, "UTF-8"); //设置文本框为读取的内容
        } catch (Exception e) {
            try {
                in.close();
                bout.close();
            } catch (Exception x) {
            }
            return "";
        }

    }

    /**
     * 文件存储与读取
     */
    public static List<Map<String, Object>> LoadFileData(String file) {
        return MyAPP.Data.toListMap(MyAPP.fileRead(MyAPP.getJsonFileName(file)));
    }

    public static List<Map<String, Object>> LoadFileData(String file, int ID) {//MyAPP.getJsonFileName(file, ID)路径名
        //MyAPP.fileRead得到json字符串
        return MyAPP.Data.toListMap(MyAPP.fileRead(MyAPP.getJsonFileName(file, ID)));//得到数据源
    }
    public static List<Itude> LoadFileData2(String file) {//MyAPP.getJsonFileName(file, ID)路径名
        //MyAPP.fileRead得到json字符串
        return MyAPP.Data.toListMap_Itude(MyAPP.fileRead(MyAPP.getJsonFileName(file)));//得到数据源
    }

    public static List<String> LoadFileData2(String file, int ID) {//MyAPP.getJsonFileName(file, ID)路径名
        //MyAPP.fileRead得到json字符串
        return MyAPP.Data.toListMap_String(MyAPP.fileRead(MyAPP.getJsonFileName(file, ID)));//得到数据源
    }

    public static void SaveFileData(String file,  List<Itude> Data) {
        fileWrite(getJsonFileName(file), MyAPP.Data.toJson_Itude(Data));
    }

    public static void SaveFileData2(String file, int ID, List<String> Data) {
        fileWrite(getJsonFileName(file, ID), MyAPP.Data.toJson2(Data));
    }

    public static void SaveFileData(String file, int ID, String Data) {
        fileWrite(getJsonFileName(file, ID), Data);
    }

    public static void SaveFileData(String file, String Data) {
        fileWrite(getJsonFileName(file), Data);
    }

}
